package com.mpyy.handler.sales;

import com.alibaba.fastjson.JSON;
import com.mpyy.pojo.*;
import com.mpyy.service.sales.SellCurrentAccountingServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Descrioption SellCurrentAccountingController 销售往来账务
 * @Author QIAO
 * @Date 2022-02-28 8:25
 */
@Controller
public class SellCurrentAccountingController {
    @Autowired
    @Qualifier(value = "sellCurrentAccountingServiceImpl")
    private SellCurrentAccountingServiceInter sellCurrentAccountingServiceInter;

    /**
     * 查询往来账务的上部分表格内的数据
     * @param request
     * @param currentAccountingCustomers
     * @return
     */
    @RequestMapping(value = {"/showCurrentAccountingDatas"})
    @ResponseBody
    public String showCurrentAccountingDatas(HttpServletRequest request , CurrentAccountingCustomers currentAccountingCustomers){
        String saleOrderId= request.getParameter("saleOrderId");
        currentAccountingCustomers.setCsalOrdId(saleOrderId);

        int total =sellCurrentAccountingServiceInter.currentAccounting(null).size();

        List<CurrentAccountingCustomers> currentAccountingList=sellCurrentAccountingServiceInter.currentAccounting(currentAccountingCustomers);


        Map<String , Object> map = new HashMap<>();
        map.put("code" , 0);
        map.put("msg","");
        map.put("count" , total);
        map.put("data" , currentAccountingList);

        String currentAccountingJosnStr= JSON.toJSONString(map);
        return currentAccountingJosnStr;
    }

    /**
     * 单据的详细信息
     * @param request
     * @param saleOrderDescAndSaleReturn
     * @return
     */
    @RequestMapping(value = {"/showDetailsOfTheDocumentDatas"})
    @ResponseBody
    public String showDetailsOfTheDocumentDatas(HttpServletRequest request , SaleOrderDescAndSaleReturn saleOrderDescAndSaleReturn){
       /* String saleOrderId= "KF3";*/

        int total =sellCurrentAccountingServiceInter.detailsOfTheDocument(null).size();

        List<SaleOrderDescAndSaleReturn> detailsOfTheDocumentList=sellCurrentAccountingServiceInter.detailsOfTheDocument(saleOrderDescAndSaleReturn);

        Map<String , Object> map = new HashMap<>();
        map.put("code" , 0);
        map.put("msg","");
        map.put("count" , total);
        map.put("data" , detailsOfTheDocumentList);

        return JSON.toJSONString(map);
    }

    /**
     * 查询客户表的数据
     * @param request
     * @param customerInfo
     * @return
     */
    @RequestMapping(value = {"/showCustomerAllInfo"})
    @ResponseBody
    public String showCustomerAllInfo(HttpServletRequest request , CustomerInfo customerInfo){

        int total =sellCurrentAccountingServiceInter.customerAllInfo(null).size();

        List<CustomerInfo> customerInfoList = sellCurrentAccountingServiceInter.customerAllInfo(customerInfo);

        Map<String , Object> map = new HashMap<>();
        map.put("code" , 0);
        map.put("msg","");
        map.put("count" , total);
        map.put("data" , customerInfoList);

        return JSON.toJSONString(map);
    }

    /**
     * 根据销售单号删除订单信息
     * @param request
     * @param saleOrder
     * @param saleOrderDesc
     * @param saleReturn
     * @param saleReturnDesc
     * @return
     */
    @RequestMapping(value = {"/deleteSaleOrderInfo"})
    @ResponseBody
    public String deleteSaleOrderInfo(HttpServletRequest request , SaleOrder saleOrder, SaleOrderDesc saleOrderDesc, SaleReturn saleReturn, SaleReturnDesc saleReturnDesc, CurrentAccountingCustomers currentAccountingCustomers){
        String csalOrdId=request.getParameter("csalOrdId");
        String substring = csalOrdId.substring(0, 2);

        if("XS".equals(substring)){
            saleOrderDesc.setSalOrdId(csalOrdId);
            saleOrder.setSalOrdId(csalOrdId);
            int deleteSaleOrderAndDescRows =sellCurrentAccountingServiceInter.deleteSaleOrderAndDescInfo(saleOrder,saleOrderDesc);
            }else if("XT".equals(substring)){
            saleReturn.setSalRetId(csalOrdId);
            saleReturnDesc.setSalRetId(csalOrdId);
            int deleteSaleReturnAndDescRows =sellCurrentAccountingServiceInter.deleteSaleReturnAndDescInfo(saleReturn,saleReturnDesc);

        }
        return showCurrentAccountingDatas(request,currentAccountingCustomers);

    }

    /**
     * 查询客户消费情况数据
     * @param request
     * @param saleOrderDesc
     * @return
     */
    @RequestMapping(value = {"/showCustomerConsumptionDatas"})
    @ResponseBody
    public String showCustomerConsumptionDatas(HttpServletRequest request , SaleOrderDesc saleOrderDesc){

        int total =sellCurrentAccountingServiceInter.customerConsumption(null).size();

        List<SaleOrderDesc> saleOrderDescsList = sellCurrentAccountingServiceInter.customerConsumption(saleOrderDesc);

        Map<String , Object> map = new HashMap<>();
        map.put("code" , 0);
        map.put("msg","");
        map.put("count" , total);
        map.put("data" , saleOrderDescsList);

        String currentAccountingJosnStr= JSON.toJSONString(map);
        return currentAccountingJosnStr;
    }

    /**
     * 查询客户账务信息数据
     * @param request
     * @param customerAccountingInformation
     * @return
     */
    @RequestMapping(value = {"/showCustomerAccountInformateDatas"})
    @ResponseBody
    public String showCustomerAccountInformateDatas(HttpServletRequest request , CustomerAccountingInformation customerAccountingInformation){

        int total =sellCurrentAccountingServiceInter.customerAccountInformate(null).size();

        List<CustomerAccountingInformation> customerAccountingInformationList = sellCurrentAccountingServiceInter.customerAccountInformate(customerAccountingInformation);

        Map<String , Object> map = new HashMap<>();
        map.put("code" , 0);
        map.put("msg","");
        map.put("count" , total);
        map.put("data" , customerAccountingInformationList);

        String currentAccountingJosnStr= JSON.toJSONString(map);
        return currentAccountingJosnStr;
    }




    /*public String searchCommodityInfo(String check){
        List<Commodity> commodities = cGstockServiceInter.searchCommodityInfo(check);
        Map<String,Object> objectMap = new HashMap<String, Object>();
        objectMap.put("code",0);
        String json=",\"data\":[";
        for (int i=0;i<commodities.size();i++){
            Commodity commodity1=commodities.get(i);
            String str= JSON.toJSONString(commodity1);
            json+=str;
            if(i<commodities.size()-1){
                json+=",";
            }
        }
        json+="]";
        String jsonString = JSON.toJSONString(objectMap);
        jsonString = jsonString.substring(0, jsonString.length() - 1);
        jsonString+=json;
        jsonString+="}";
        return jsonString;


        String json = ",\"data\":[";
        for ( int i = 0 ; i < detailsOfTheDocumentList.size() ; i++ ) {
            SaleOrderDesc saleOrderDesc1 = detailsOfTheDocumentList.get(i);
            String str = JSON.toJSONString(saleOrderDesc1);
            json += str;
            if ( i < detailsOfTheDocumentList.size()-1 ) {
                json += ",";
            }
        }
        json+="]";

        String detailsOfTheDocumentJosnStr= JSON.toJSONString(map);
        detailsOfTheDocumentJosnStr =
                detailsOfTheDocumentJosnStr.substring(0 , detailsOfTheDocumentJosnStr.length()-1);
        detailsOfTheDocumentJosnStr += json;
        detailsOfTheDocumentJosnStr += "}";
    }*/
}
